/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * Parse the time to string
 * @param {(Object|string|number)} time
 * @param {string} cFormat
 * @returns {string}
 */
export function parseTime(time, cFormat) {
  if (arguments.length === 0) {
    return null;
  }
  const format = cFormat || "{y}-{m}-{d} {h}:{i}:{s}";
  let date;
  if (typeof time === "object") {
    date = time;
  } else {
    if (typeof time === "string" && /^[0-9]+$/.test(time)) {
      time = parseInt(time);
    }
    if (typeof time === "number" && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay()
  };
  const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
    let value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === "a") {
      return ["日", "一", "二", "三", "四", "五", "六"][value];
    }
    if (result.length > 0 && value < 10) {
      value = "0" + value;
    }
    return value || 0;
  });
  return time_str;
}

/**
 * @param {number} time
 * @param {string} option
 * @returns {string}
 */
export function formatTime(time, option) {
  if (("" + time).length === 10) {
    time = parseInt(time) * 1000;
  } else {
    time = +time;
  }
  const d = new Date(time);
  const now = Date.now();

  const diff = (now - d) / 1000;

  if (diff < 30) {
    return "刚刚";
  } else if (diff < 3600) {
    // less 1 hour
    return Math.ceil(diff / 60) + "分钟前";
  } else if (diff < 3600 * 24) {
    return Math.ceil(diff / 3600) + "小时前";
  } else if (diff < 3600 * 24 * 2) {
    return "1天前";
  }
  if (option) {
    return parseTime(time, option);
  } else {
    return (
      d.getMonth() +
      1 +
      "月" +
      d.getDate() +
      "日" +
      d.getHours() +
      "时" +
      d.getMinutes() +
      "分"
    );
  }
}

/**
 * @param {string} url
 * @returns {Object}
 */
export function param2Obj(url) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, " ") +
      '"}'
  );
}
/**
 * This is just a simple version of deep copy
 * Has a lot of edge cases bug
 * If you want to use a perfect deep copy, use lodash's _.cloneDeep
 * @param {Object} source
 * @returns {Object}
 */
export function deepClone(source) {
  if (!source && typeof source !== "object") {
    throw new Error("error arguments", "deepClone");
  }
  const targetObj = source.constructor === Array ? [] : {};
  Object.keys(source).forEach(keys => {
    if (source[keys] && typeof source[keys] === "object") {
      targetObj[keys] = deepClone(source[keys]);
    } else {
      targetObj[keys] = source[keys];
    }
  });
  return targetObj;
}

/***
 * @param func 输入完成的回调函数
 * @param delay 延迟时间
 */
export function debounce(func, delay) {
  let timer;
  return (...args) => {
    // console.log(...args)
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      func.apply(this, args);
    }, delay);
  };
}
function fun_dates(num) {
  let today = new Date();
  let lastMonth = new Date(today.getFullYear(), today.getMonth() - 1);
  let year = lastMonth.getFullYear();
  let month =
    lastMonth.getMonth() + 1 >= 10
      ? lastMonth.getMonth() + 1
      : "0" + (lastMonth.getMonth() + 1);
  return year + "-" + month + "-" + num;
}
/***
 * @param func 输入完成的回调函数
 * @param delay 延迟时间
 */
function fun_date(num) {
  var date1 = new Date();
  //今天时间
  var time1 =
    date1.getFullYear() + "-" + (date1.getMonth() + 1) + "-" + date1.getDate();
  var date2 = new Date(date1);
  date2.setDate(date1.getDate() - num);
  //num是正数表示之后的时间，num负数表示之前的时间，0表示今天
  var time2 =
    date2.getFullYear() + "-" + (date2.getMonth() + 1) + "-" + date2.getDate();
  return time2;
}
export function getnowDate() {
  var time = new Date().getTime() - 24 * 60 * 60 * 1000;
  var yesterday = new Date(time);
  var month = yesterday.getMonth();
  var day = yesterday.getDate();
  yesterday =
    yesterday.getFullYear() +
    "-" +
    (yesterday.getMonth() > 9
      ? yesterday.getMonth() + 1
      : "0" + (yesterday.getMonth() + 1)) +
    "-" +
    (yesterday.getDate() > 9 ? yesterday.getDate() : "0" + yesterday.getDate());
  console.log(yesterday, 123243);
  let endtimer,
    endtimers,
    startimer,
    settlementStarTimer,
    settlementStarTimers,
    newStarTimer;
  let nowDate = new Date();
  let date = {
    year: nowDate.getFullYear(),
    month:
      nowDate.getMonth() + 1 >= 10
        ? nowDate.getMonth() + 1
        : "0" + (nowDate.getMonth() + 1),
    date:
      nowDate.getDate() + 1 >= 10 ? nowDate.getDate() : "0" + nowDate.getDate()
  };
  if (date.date == 32) {
    date.date = 31;
  }
  startimer = date.year + "-" + date.month + "-" + "01";
  endtimer = date.year + "-" + date.month + "-" + date.date;
  endtimers = date.year + "-" + date.month + "-" + 20;
  newStarTimer = fun_date(7);
  settlementStarTimer = fun_date(30);
  settlementStarTimers = fun_dates(21);
  return {
    yesterday,
    startimer,
    endtimer,
    endtimers,
    newStarTimer,
    settlementStarTimer,
    settlementStarTimers
  };
}
